package com.shundr.shipper.frame.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.text.TextUtils;

/**
 * 字符串处理工具类
 * @author larry
 *
 */
public class StringUtil {
	
	private static Pattern pattern = Pattern.compile("\\S*[?]\\S*"); 
	
	public static final String STRING_YES = "Y";
	public static final String STRING_NO = "N";
	public static String SMS_PATTERN_CODER = "(?<!\\d)\\d{6}(?!\\d)";

	/**
	 * 是否为空字符�?
	 * @param str
	 * @return
	 */
	public static final boolean isNull(String str) {
		return  str == null || str.trim().equals("null") || "".equals(str.trim());
	}
	
	public static final boolean isEqual(String str1, String str2){
		if(str1 == null || str2 == null){
			return false;
		}
		
		return str1.equals(str2);
	}

	public static boolean ynToBoolean(String str) {
		return STRING_YES.equals(str) ? true : false;
	}

	public static Boolean ynToBooleanObject(String str) {
		if(STRING_YES.equals(str)) {
			return true;
		} else if(STRING_NO.equals(str)) {
			return false;
		} else {
			return null;
		}
	}
	
	/**
	 * 判断是否是当天数�?
	 * @param date
	 * @return
	 */
	public static boolean isToday(Date date){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String today = format.format(new Date(System.currentTimeMillis()));
		String dateStr = format.format(date);
		if(today.equals(dateStr)){
			return true;
		}
		return false;
	}
	
	/**
	 * 判断字符串是不是数字
	 * @param str
	 * @return
	 */
	public static boolean isNumeric(String str) {
		Pattern pattern = Pattern.compile("[0-9]*");
		Matcher isNum = pattern.matcher(str);
		if (!isNum.matches()) {
			return false;
		}
		return true;
	}
	
	/**
	 * 判断用户名是否合�?
	 * */
	public static boolean accountIsLegal(String str) {
		try {
			Pattern pattern = Pattern.compile("^[a-zA-Z][a-zA-Z0-9_-]*$");
			return pattern.matcher(str).matches();
		} catch (Exception e) {

		}
		return false;
	}
	
	/**
	 * 判断是否是合法手机号
	 */
	public static boolean isMobileNO(String mobiles) {
		try {
			 Pattern p = Pattern.compile("^((13[0-9])|(147)|(15[0-9])|(17[0-9])|(18[0-9]))\\d{8}$");
			return p.matcher(mobiles).matches();
		} catch (Exception e) {

		}
		return false;
	}
	
	/**
	 * 判断车牌号是否合法
	 * @param num
	 * @return
	 */
	public static boolean isTruckNum(String num) {
		try {
			Pattern p = Pattern.compile("^[\u4e00-\u9fa5]{1}[a-zA-Z]{1}[a-zA-Z_0-9]{4}[a-zA-Z_0-9_\u4e00-\u9fa5]$");
			return p.matcher(num).matches();
		} catch (Exception e) {

		}
		return false;
	}
	

	/**
	 * 验证输入的邮箱格式是否符�?
	 */
	public static boolean isEmail(String email) {
		try {
			String emailPattern = "[a-zA-Z0-9][a-zA-Z0-9._-]{2,16}[a-zA-Z0-9]@[a-zA-Z0-9]+.[a-zA-Z0-9]+";
			return Pattern.matches(emailPattern, email);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * 获取链接的后缀名
	 * 
	 * @return
	 */
	public static String parseSuffix(String url) {
		Matcher matcher = pattern.matcher(url);
		String[] spUrl = url.toString().split("/");
		int len = spUrl.length;
		String endUrl = spUrl[len - 1];
		if (matcher.find()) {
			String[] spEndUrl = endUrl.split("\\?");
			return spEndUrl[0].split("\\.")[1];
		}
		return endUrl.split("\\.")[1];
	}
	
	public static String getUrlName(String url){
		return url.substring(url.lastIndexOf("/") + 1);
	}
	
	/**
	 * 验证身份证号码
	 * checkIDCard(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param card
	 * @return
	 * boolean
	 * @exception
	 * @since  1.0.0
	 */
	public static boolean checkIDCard(String card){
		//定义判别用户身份证号的正则表达式（要么是15位，要么是18位，最后一位可以为字母）  
        Pattern idNumPattern = Pattern.compile("(\\d{14}[0-9a-zA-Z])|(\\d{17}[0-9a-zA-Z])");  
        //通过Pattern获得Matcher  
        Matcher idNumMatcher = idNumPattern.matcher(card);  
        //判断用户输入是否为身份证号  
        if(idNumMatcher.matches()){  
            System.out.println("您的出生年月日是：");  
            //如果是，定义正则表达式提取出身份证中的出生日期  
            Pattern birthDatePattern= Pattern.compile("\\d{6}(\\d{4})(\\d{2})(\\d{2}).*");//身份证上的前6位以及出生年月日  
            //通过Pattern获得Matcher  
            Matcher birthDateMather= birthDatePattern.matcher(card);  
            //通过Matcher获得用户的出生年月日  
            if(birthDateMather.find()){  
                String year = birthDateMather.group(1);  
                String month = birthDateMather.group(2);  
                String date = birthDateMather.group(3);  
                //输出用户的出生年月日  
                System.out.println(year+"年"+month+"月"+date+"日");                  
            }   
            return true;
        }else{  
            //如果不是，输出信息提示用户  
            System.out.println("您输入的并不是身份证号");
            return false;
        }  
	}
	
	/**
	 * 匹配短信中间的6个数字（验证码等）
	 * 
	 * @param patternContent
	 * @return
	 */
	public static String patternSmsCode(String patternContent) {
		if (TextUtils.isEmpty(patternContent)) {
			return null;
		}
		Pattern p = Pattern.compile(SMS_PATTERN_CODER);
		Matcher matcher = p.matcher(patternContent);
		if (matcher.find()) {
			return matcher.group();
		}
		return null;
	}
	
	public static String getDateStr(int date) {
		if (date < 10) {
			return "0" + date + "";
		} else {
			return date + "";
		}
	}
	
}
